package com.sky.controller.user;

import com.sky.constant.StatusConstant;
import com.sky.entity.Dish;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: ggm
 * @data 2024年12月09日 20:20
 */
@Slf4j
@RestController("userDishController")
@RequestMapping("/user/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;
     /**
      * @Author: ggm
      * @date:  2024/12/9
      * 获取菜品详情
      */
     @GetMapping("/list")
     public Result<List<DishVO>> getById(Long categoryId){
         log.info("获取菜品详情{}", categoryId);
         //查询redis缓存，key为dish_+ categoryId
         String key = "dish_"+categoryId;
         List<DishVO>redisList = (List<DishVO>) redisTemplate.opsForValue().get(key);
         //如果存在，直接返回
         if(redisList!=null&&redisList.size()>0){
             return Result.success(redisList);
         }
         //如果不存在，查询数据库
         Dish dish = new Dish();
         dish.setCategoryId(categoryId);
         dish.setStatus(StatusConstant.ENABLE);
         List<DishVO> list = dishService.listWithFlavor(dish);
         //将查询结果存入redis
         redisTemplate.opsForValue().set(key,list);
         return Result.success(list);


     }

}
